/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uca.sisbec.actions.becario;

import com.uca.sisbec.commons.HibernateAction;
import com.uca.sisbec.commons.Utils;
import com.uca.sisbec.forms.manto.BecarioForm;
import com.uca.sisbec.helpers.manto.BecarioHelper;
import com.uca.sisbec.mapping.Becario;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Juank
 */
public class BecarioIndex extends HibernateAction{
    public ActionForward init (ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) {
        /*if (request.getSession().getAttribute(_BECARIO) != null)
            return mapping.findForward("success");*/
        return mapping.findForward("search");
    }

    public ActionForward BuscarBecario(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        BecarioForm bf = (BecarioForm) form;
        BecarioHelper helper = new BecarioHelper(getSession(request));
        request.setAttribute("becarioData", helper.buscarBecarioDB(bf));
        return mapping.findForward("search");
    }

    public ActionForward sesionBecario(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        BecarioForm bf = (BecarioForm) form;
        BecarioHelper helper = new BecarioHelper(getSession(request));
        Becario becario = helper.buscarBecarioDB(bf.getCarnet());
        becario.setFoto(!Utils.isNullOrEmpty(becario.getFoto()) ? becario.getFoto() : "");
        request.getSession().setAttribute(_BECARIO, becario);
        return mapping.findForward("success");
    }

    public ActionForward init2(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        if (!isBecarioInSession(request)) return mapping.findForward("searchBecario");
        return mapping.findForward("success");
    }

    public ActionForward cambioRapidoBecario(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        BecarioForm bf = (BecarioForm) form;
        BecarioHelper helper = new BecarioHelper(getSession(request));
        Becario becario = helper.buscarBecarioDB(bf.getCarnet());
        if (becario != null){
            request.getSession().removeAttribute(_BECARIO);
            request.getSession().setAttribute(_BECARIO, becario);
        } else {
            bf.setCarnet("");
            return init2(mapping, form, request, response);
        }
        bf.setCarnet("");
        return mapping.findForward("success");
    }
}
